//
//  ViewController.m
//  CoreDataDemo
//
//  Created by chenguibang on 16/2/26.
//  Copyright © 2016年 chenguibang. All rights reserved.
//

#import "ViewController.h"
#import "UserEntity.h"
#import "CDManager.h"
#import <mach/mach_time.h>
#import "CacheDAO.h"
#import "TestRelationShip.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    NSArray *titleArr = [NSArray arrayWithObjects:@"add",@"del",@"update",@"query",@"test2", nil];
    for (int i = 0; i< titleArr.count; i++) {
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10, 35*(i+2), 150, 33)];
        [btn setTitle:titleArr[i] forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor orangeColor];
        [btn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchDown];
        [self.view addSubview:btn];
    }
}


-(void)btn:(UIButton*)ss{
    if ([ss.titleLabel.text isEqualToString:@"add"]) {
        CacheDAO *dao = [[CacheDAO alloc]init];
        
        RspCacheModel *che = [[RspCacheModel alloc]init];
        che.requrlstr = @"http://www.baidu.com";
        [dao insert:che];
        
        
        
    }else if ([ss.titleLabel.text isEqualToString:@"del"]){
        
        CacheDAO *dao = [[CacheDAO alloc]init];
        
        RspCacheModel *che = [[RspCacheModel alloc]init];
        che.requrlstr = @"http://www.baidu.com";
        [dao remove:che];
        
        
        
        return;
        CDManager *manager = [CDManager shareManager];
        
        //初始化一个查询请求
        NSFetchRequest *requst = [[NSFetchRequest alloc]init];
        //设置要查询的实体
        requst.entity = [NSEntityDescription entityForName:@"UserEntity" inManagedObjectContext:manager.managedObjectContext];
        //设置排序 按age降序
        NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:NO];
        requst.sortDescriptors = [NSArray arrayWithObject:sort];
        //
        // 设置条件过滤(搜索name中包含字符串"Itcast-1"的记录，注意：设置条件过滤时，数据库SQL语句中的%要用*来代替，所以%Itcast-1%应该写成*Itcast-1*)
        NSPredicate *prediction = [NSPredicate predicateWithFormat:@"name = %@",@"张珊珊"];
        requst.predicate = prediction;
        //
        NSError *error3 = nil;
        //执行请求
        NSArray *objs = [manager.managedObjectContext executeFetchRequest:requst error:&error3];
        if (error3) {
            [NSException raise:@"查询错误" format:@"%@", [error3 localizedDescription]];
        }
        for(UserEntity *userEntity  in objs){
            NSLog(@"name = %@  is  %@  years old",userEntity.name,userEntity.name);
//            [manager.managedObjectContext delete:userEntity];
            [manager.managedObjectContext deleteObject:userEntity];
        }
        
        
        
        
    }else if ([ss.titleLabel.text isEqualToString:@"update"]){
        
        CacheDAO *dao = [[CacheDAO alloc]init];
        RspCacheModel *che = [[RspCacheModel alloc]init];
        che.requrlstr = @"http://www.hao123.com";
        [dao modify:che];
        
    }else if ([ss.titleLabel.text isEqualToString:@"query"]){
        
        CacheDAO *dao = [[CacheDAO alloc]init];
        NSMutableArray *arr = [dao findAll];
        NSLog(@"%@",arr);
        for (RspCacheModel *model in arr) {
            NSLog(@"%@",model.requrlstr);
        }
        
        
        
        return;
        CDManager *manager = [CDManager shareManager];
        uint64_t begin = mach_absolute_time();
        
        NSArray *a = [manager queryWithEntity:@"UserEntity" key:@"name" value:@"张珊珊"];
        
        uint64_t end = mach_absolute_time();
        
        
        NSLog(@"%llu",end - begin);
        for(UserEntity *userEntity  in a){
            NSLog(@"name = %@  is  %@  years old  %@",userEntity.name,userEntity.name,userEntity.image);
            NSData *data = userEntity.image;
            UIImage *image = [UIImage imageWithData:data];
            UIImageView *ima = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
            [self.view addSubview:ima];
            ima.image = image;
        }
        
        
        
        return;
        
        
        //初始化一个查询请求
        NSFetchRequest *requst = [[NSFetchRequest alloc]init];
        //设置要查询的实体
        requst.entity = [NSEntityDescription entityForName:@"UserEntity" inManagedObjectContext:manager.managedObjectContext];
        //设置排序 按age降序
        NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:NO];
        requst.sortDescriptors = [NSArray arrayWithObject:sort];
        //
        // 设置条件过滤(搜索name中包含字符串"Itcast-1"的记录，注意：设置条件过滤时，数据库SQL语句中的%要用*来代替，所以%Itcast-1%应该写成*Itcast-1*)
        NSPredicate *prediction = [NSPredicate predicateWithFormat:@"name like %@",@"*张*"];
        
        requst.predicate = prediction;
        //
        NSError *error3 = nil;
        //执行请求
        NSArray *objs = [manager.managedObjectContext executeFetchRequest:requst error:&error3];
        if (error3) {
            [NSException raise:@"查询错误" format:@"%@", [error3 localizedDescription]];
        }
        for(UserEntity *userEntity  in objs){
            NSLog(@"name = %@  is  %@  years old  %@",userEntity.name,userEntity.name,userEntity.image);
            
            NSData *data = userEntity.image;
            
            UIImage *image = [UIImage imageWithData:data];
            
            UIImageView *ima = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
            [self.view addSubview:ima];
            ima.image = image;
        }
    }else if ([ss.titleLabel.text isEqualToString:@"test2"]){
        TestRelationShip *t = [[TestRelationShip alloc]init];
       [self presentViewController:t animated:NO completion:^{
           
       }];
        
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
